<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="130" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <multiuse-button type="primary" :click="saveHandle.bind(this, 1)">通 过</multiuse-button>
      <multiuse-button type="primary" :click="saveHandle.bind(this, 2)">驳 回</multiuse-button>
      <el-button @click="closeDrawer">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { detailInfo, saveData, getAmount } from '@/api/omFinance/RebateRedInvoice/RebateRedInvoiceApply';
import { batchAudit } from '@/api/omFinance/RebateRedInvoice/RebateRedInvoiceApprove';
import { getUser } from '@/assets/js/auth';
import { isArcfox } from '@/api/cms-api/origin/commonDealerType';

export default {
  name: 'SonModel',
  mixins: [dictionary],

  props: ['type', 'rowData'],
  components: {},
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      formData: {},
      isDealer: false,
      taxRate1: 0,
      Amount1: 0,
      taxRate2: 0,
      Amount2: 0
    };
  },

  mounted() { },
  created() {
    // this.init();
    if (isArcfox()) {
      this.formList.find(x => x.fieldName == 'salesOrgCode').itemList = [{ text: '北京蓝谷极狐投资管理有限公司销售组织', value: '1012' }];
    } else {
      this.formList.find(x => x.fieldName == 'salesOrgCode').itemList = [
        { text: '北京汽车销售有限公司', value: '1080' },
        { text: '株洲北汽汽车销售有限公司', value: '1180' },
        { text: '北京新能源汽车营销有限公司', value: '2000' },
        { text: '青岛卫蓝销售公司', value: '2001' }
      ];
    }
    this.getInfoById();
  },
  computed: {},
  methods: {
    init() {
      for (let i in this.rowData) {
        this.formList.map(x => {
          if (x.fieldName == i) {
            x.initialValue = this.rowData[i];
          }
        });
      }
      // if (this.type == 'show') {
      //   this.formList.map(x => {
      //     if (x.rules) {
      //       x.rules[0].required = false;
      //     }
      //   });
      // }
    },
    // 详细
    async getInfoById() {
      const res = await detailInfo(this.rowData.id);
      if (res.resultCode == 200) {
        this.formList.map(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.getAmount(this.rowData.powerType, this.rowData.rebateType, this.rowData.dealerCode);
        this.getAmountD(this.rowData.powerType, this.rowData.rebateTypeD, this.rowData.dealerCode);
      }
    },

    dealerHandle() {
      console.log('---经销商---');
      this.isDealer = true;
    },
    emitcloseHandler() { },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveHandle(type) {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        if (type == 1) {
          let str = this.formList.find(x => x.fieldName == 'checkDesc').initialValue;
          let json = { checkDesc: str };
          json.auditType = 1;
          json.rebateRedApplyIds = [this.rowData.id];
          let res = await batchAudit(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '通过成功',
              type: 'success'
            });
            this.closeDrawer('refresh');
          }
        } else {
          let str = this.formList.find(x => x.fieldName == 'checkDesc').initialValue;
          if (!str) {
            this.$notify({
              title: '警告',
              message: '驳回请输入驳回意见',
              type: 'warning'
            });
            return;
          }
          let json = { checkDesc: str };
          json.auditType = 2;
          json.rebateRedApplyIds = [this.rowData.id];
          let res = await batchAudit(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '驳回成功',
              type: 'success'
            });
            this.closeDrawer('refresh');
          }
        }
      }
    },
    closeDrawer(type) {
      this.$emit('close', type);
    },

    async getAmount(powerType, rebateType, dealerCode) {
      if (!powerType || !rebateType || !dealerCode) {
        return;
      } else {
        let res = await getAmount({ powerType: powerType, rebateType: rebateType, dealerCode: dealerCode });
        this.formList.find(x => x.fieldName == 'totalAmount').initialValue = res.data;
      }
    },
    async getAmountD(powerType, rebateType, dealerCode) {
      if (!powerType || !rebateType || !dealerCode) {
        return;
      } else {
        let res = await getAmount({ powerType: powerType, rebateType: rebateType, dealerCode: dealerCode });
        this.formList.find(x => x.fieldName == 'totalAmountD').initialValue = res.data;
      }
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '申请单号',
          fieldName: 'applyNo',
          placeholder: '系统自动生成',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '',
          readonly: true,
          disabled: true,
          rules: [{ required: true, message: '请选择经销商', trigger: 'change' }],
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={this.dealerHandle}></el-button>;
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入',
          rules: [{ required: true, message: '请选择经销商', trigger: 'change' }],
          disabled: true
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008'),
          initialValue: 60081002,
          disabled: true,
          offsetRightCols: 2,
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: val => {
            // this.changeCompany(val)
            if (val) {
              let powerType = val;
              let rebateType = this.formList.find(x => x.fieldName == 'rebateType').initialValue;
              let dealerCode = this.formList.find(x => x.fieldName == 'dealerCode').initialValue;
              this.getAmount(powerType, rebateType, dealerCode);
              this.getSalesOrgCode(val);
              return;
            }
            this.formList.find(x => x.fieldName == 'salesOrgCode').initialValue = '';
            this.formList.find(x => x.fieldName == 'salesOrgCode').itemList = [];
          }
        },
        {
          type: 'SELECT',
          label: '公司代码',
          fieldName: 'salesOrgCode',
          placeholder: '请输入',
          itemList: [],
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: p => { }
        },
        {
          type: 'DATE',
          label: '申请时间',
          fieldName: 'applyDate',
          // dateType: 'exactdate',
          placeholder: '请输入',
          disabled: true,
          initialValue: moment().format('YYYY-MM-DD HH:mm:ss')
        },

        {
          type: 'INPUT',
          label: '申请人',
          fieldName: 'applyPersonName',
          placeholder: '自动带出',
          initialValue: getUser(),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '总申请金额',
          fieldName: 'totalApplyAmount',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
          offsetRightCols: 2,
          disabled: true,
          change: val => {
            if (!this.formList.find(x => x.fieldName == 'includeTaxAmount1').initialValue && !this.formList.find(x => x.fieldName == 'includeTaxAmount2').initialValue) {
              this.formList.find(x => x.fieldName == 'includeTaxAmount1').initialValue = parseFloat(val);
              this.taxRate1 = parseFloat(this.formList.find(x => x.fieldName == 'taxRate1').initialValue);
              this.Amount1 = parseFloat(val);
              this.taxAmount1;
              // this.formList.find(x => x.fieldName == 'includeTaxAmount2').initialValue = parseFloat(val);
              // this.taxRate2 = parseFloat(this.formList.find(x => x.fieldName == 'taxRate2').initialValue);
              // this.Amount2 = parseFloat(val);
              // this.taxAmount2;
            }
          }
        },
        {
          type: 'SELECT',
          label: '返利类型',
          fieldName: 'rebateType',
          placeholder: '请选择',
          disabled: true,
          itemList: this.createDictList('6036'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: val => {
            let powerType = this.formList.find(x => x.fieldName == 'powerType').initialValue;
            let rebateType = val;
            let dealerCode = this.formList.find(x => x.fieldName == 'dealerCode').initialValue;
            this.getAmount(powerType, rebateType, dealerCode);
          }
        },
        {
          type: 'INPUT',
          label: '返利可用余额',
          fieldName: 'totalAmount',
          placeholder: '',
          offsetRightCols: 1,
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '税率1（%）',
          fieldName: 'taxRate1',
          placeholder: '请输入',
          initialValue: 13,
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
          onInput: val => {
            this.taxRate1 = parseFloat(val) || 0;
            this.Amount1 = parseFloat(this.formList.find(x => x.fieldName == 'includeTaxAmount1').initialValue) || 0;
            this.taxAmount1;
          }
        },
        {
          type: 'INPUT',
          label: '金额1（含税）',
          fieldName: 'includeTaxAmount1',
          placeholder: '请输入',
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
          onInput: val => {
            this.Amount1 = parseFloat(val);
            this.taxRate1 = parseFloat(this.formList.find(x => x.fieldName == 'taxRate1').initialValue) || 0;
            this.taxAmount1;
          }
        },
        {
          type: 'INPUT',
          label: '税额1',
          fieldName: 'taxAmount1',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '不含税金额1',
          fieldName: 'outcluedeTaxAmount1',
          placeholder: '',
          disabled: true,
          offsetRightCols: 2
        },

        {
          type: 'INPUT',
          label: '税率2（%）',
          fieldName: 'taxRate2',
          placeholder: '请输入',
          initialValue: '',
          disabled: true,
          onInput: val => {
            this.taxRate2 = parseFloat(val) || 0;
            this.Amount2 = parseFloat(this.formList.find(x => x.fieldName == 'includeTaxAmount2').initialValue) || 0;
            this.taxAmount2;
          }
        },
        {
          type: 'INPUT',
          label: '金额2（含税）',
          fieldName: 'includeTaxAmount2',
          placeholder: '请输入',
          disabled: true,
          onInput: val => {
            // let totalAmount = this.formList.find(x => x.fieldName == 'totalApplyAmount').initialValue || 0;
            // let Amount1 = totalAmount > 0 ? parseFloat(totalAmount) - parseFloat(val) : 0;
            // this.formList.find(x => x.fieldName == 'includeTaxAmount1').initialValue = parseFloat(Amount1);
            // this.taxRate1 = parseFloat(this.formList.find(x => x.fieldName == 'taxRate1').initialValue) || 0;
            // this.Amount1 = parseFloat(this.formList.find(x => x.fieldName == 'includeTaxAmount1').initialValue) || 0;
            // this.taxAmount1;

            this.Amount2 = parseFloat(val) || 0;
            this.taxRate2 = parseFloat(this.formList.find(x => x.fieldName == 'taxRate2').initialValue) || 0;
            this.taxAmount2;
          }
        },
        {
          type: 'INPUT',
          label: '税额2',
          fieldName: 'taxAmount2',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '不含税金额2',
          fieldName: 'outcluedeTaxAmount2',
          placeholder: '',
          disabled: true,
          offsetRightCols: 2
        },
        {
          type: 'BREAK_SPACE',
          label: ''
        },
        {
          type: 'INPUT',
          label: '总申请金额',
          fieldName: 'totalApplyAmountD',
          placeholder: '请输入',
          disabled: true,
          offsetRightCols: 2,
          change: val => {
            if (!this.formList.find(x => x.fieldName == 'includeTaxAmount1D').initialValue && !this.formList.find(x => x.fieldName == 'includeTaxAmount2D').initialValue) {
              this.formList.find(x => x.fieldName == 'includeTaxAmount1D').initialValue = parseFloat(val);
              this.taxRate1D = parseFloat(this.formList.find(x => x.fieldName == 'taxRate1D').initialValue);
              this.Amount1D = parseFloat(val);
              this.taxAmount1D;
            }
          }
        },
        // {
        //   type: 'SELECT',
        //   label: '动力形式',
        //   fieldName: 'powerTypeD',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('6008'),
        //   initialValue: 60081002,
        //   disabled: true,
        //   change: val => {
        //     // this.changeCompany(val)
        //     if (val) {
        //       let powerTypeD = val;
        //       let rebateTypeD = this.formList.find(x => x.fieldName == 'rebateTypeD').initialValue;
        //       let dealerCode = this.formList.find(x => x.fieldName == 'dealerCode').initialValue;
        //       this.getAmountD(powerTypeD, rebateTypeD, dealerCode);
        //       this.getSalesOrgCode(val);
        //       return;
        //     }
        //     this.formList.find(x => x.fieldName == 'salesOrgCode').initialValue = '';
        //     this.formList.find(x => x.fieldName == 'salesOrgCode').itemList = [];
        //   }
        // },
        {
          type: 'SELECT',
          label: '返利类型',
          fieldName: 'rebateTypeD',
          placeholder: '请选择',
          disabled: true,
          itemList: this.createDictList('6036'),
          change: val => {
            let powerType = this.formList.find(x => x.fieldName == 'powerType').initialValue;
            let rebateTypeD = val;
            let dealerCode = this.formList.find(x => x.fieldName == 'dealerCode').initialValue;
            this.getAmountD(powerType, rebateTypeD, dealerCode);
          }
        },
        {
          type: 'INPUT',
          label: '返利可用余额',
          fieldName: 'totalAmountD',
          placeholder: '',
          offsetRightCols: 1,
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '税率1（%）',
          fieldName: 'taxRate1D',
          placeholder: '请输入',
          initialValue: 13,
          disabled: true,
          onInput: val => {
            this.taxRate1D = parseFloat(val) || 0;
            this.Amount1D = parseFloat(this.formList.find(x => x.fieldName == 'includeTaxAmount1D').initialValue) || 0;
            this.taxAmount1D;
          }
        },
        {
          type: 'INPUT',
          label: '金额1（含税）',
          fieldName: 'includeTaxAmount1D',
          placeholder: '请输入',
          disabled: true,
          onInput: val => {
            this.Amount1D = parseFloat(val);
            this.taxRate1D = parseFloat(this.formList.find(x => x.fieldName == 'taxRate1D').initialValue) || 0;
            this.taxAmount1D;
          }
        },
        {
          type: 'INPUT',
          label: '税额1',
          fieldName: 'taxAmount1D',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '不含税金额1',
          fieldName: 'outcluedeTaxAmount1D',
          placeholder: '',
          disabled: true,
          offsetRightCols: 2
        },

        {
          type: 'INPUT',
          label: '税率2（%）',
          fieldName: 'taxRate2D',
          placeholder: '请输入',
          initialValue: '',
          disabled: true,
          onInput: val => {
            this.taxRate2D = parseFloat(val) || 0;
            this.Amount2D = parseFloat(this.formList.find(x => x.fieldName == 'includeTaxAmount2D').initialValue) || 0;
            this.taxAmount2D;
          }
        },
        {
          type: 'INPUT',
          label: '金额2（含税）',
          fieldName: 'includeTaxAmount2D',
          placeholder: '请输入',
          disabled: true,
          onInput: val => {
            // let totalAmount = this.formList.find(x => x.fieldName == 'totalApplyAmount').initialValue || 0;
            // let Amount1 = totalAmount > 0 ? parseFloat(totalAmount) - parseFloat(val) : 0;
            // this.formList.find(x => x.fieldName == 'includeTaxAmount1').initialValue = parseFloat(Amount1);
            // this.taxRate1 = parseFloat(this.formList.find(x => x.fieldName == 'taxRate1').initialValue) || 0;
            // this.Amount1 = parseFloat(this.formList.find(x => x.fieldName == 'includeTaxAmount1').initialValue) || 0;
            // this.taxAmount1;

            this.Amount2D = parseFloat(val) || 0;
            this.taxRate2D = parseFloat(this.formList.find(x => x.fieldName == 'taxRate2D').initialValue) || 0;
            this.taxAmount2D;
          }
        },
        {
          type: 'INPUT',
          label: '税额2',
          fieldName: 'taxAmount2D',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '不含税金额2',
          fieldName: 'outcluedeTaxAmount2D',
          placeholder: '',
          disabled: true,
          offsetRightCols: 2
        },
        {
          type: 'BREAK_SPACE',
          label: ''
        },
        {
          type: 'INPUT',
          label: '红字发票信息表编号',
          fieldName: 'remark',
          selfCols: 3,
          placeholder: '请输入',
          disabled: true,
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '审批意见',
          fieldName: 'checkDesc',
          selfCols: 3,
          placeholder: '请输入'
        }
      ];
    }
  }
};
</script>
